<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Cache;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PtypeController extends Controller
{
    /**
     * 展示项目分类
     *
     * @return object
     */
    public function getIndex($id)
    {
        //查询数据库
        $ptypes = DB::table('ptype')->where('pid',$id)->get();
        $pcontent = [];
        foreach ($ptypes as $k => $v) {
            $pcontent[] = DB::table('pcontent')->where('pid',$v->id)->orderBy('time', 'desc')->take(2)->get();
        }
        //获取缓存数据
        $value = Cache::get('head');
        
        return view('Home.ptype.index',['type'=>$value['type'],'config'=>$value['config'],'blinks'=>$value['blinks'],'datas'=>$value['datas'],'ptype'=>$value['ptype'],'ptypes'=>$ptypes,'pts'=>$value['pts'],'pcontent'=>$pcontent]);
    }

   /**
     * 展示单一项目分类及内容
     *
     * @return object
     */
    public function getInfo($id)
    {
        //查询数据库
        $ptypes = DB::table('ptype')->where('id',$id)->first();
        $pcontent = DB::table('pcontent')->where('pid',$id)->orderBy('time', 'desc')->take(6)->get();
        $pcontents = DB::table('pcontent')->where('pid',$id)->orderBy('time', 'desc')->paginate(5);
        $pcons = DB::table('pcontent')->where('pid',$id)->orderBy('time', 'desc')->take(8)->get();
        $activities = DB::table('activities')->where('sort','1')->take(8)->get();
        //存入缓存
        Cache::forget('pcon');
        $res = Cache::add('pcon', ['pcons'=>$pcons,'acts'=>$activities], 1440);
        //获取缓存数据
        $value = Cache::get('head');

        return view('Home.ptype.info',['type'=>$value['type'],'config'=>$value['config'],'blinks'=>$value['blinks'],'datas'=>$value['datas'],'ptype'=>$value['ptype'],'pts'=>$value['pts'],'ptypes'=>$ptypes,'pcontent'=>$pcontent,'pcontents'=>$pcontents,'pcons'=>$pcons,'acts'=>$activities]);
    }
}
